package cn.iocoder.yudao.module.wecom.mq.consumer;

import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.wecom.mq.message.SyncCustomerMessage;
import cn.iocoder.yudao.module.wecom.service.customer.WeCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 针对 {@link SyncCustomerMessage} 的消费者
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class SyncCustomerConsumer {


    @Resource
    private WeCustomerService weCustomerService;


    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(SyncCustomerMessage message) {
        log.info("[SyncCustomerConsumer][消息内容({})]", message);
        TenantUtils.execute(message.getTenantId(), () -> {
            weCustomerService.syncCustomerByMQ();
        });
    }

}
